//! 简单的WebSocket连接测试
//! 
//! 测试基本的WebSocket连接功能

use binance_sdk_unified::{
    client::BinanceClientBuilder, ProductType, Environment, Result,
};
use std::time::Duration;
use tokio::time::sleep;

#[tokio::main]
async fn main() -> Result<()> {
    // 初始化日志
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::INFO)
        .init();

    println!("🔧 简单WebSocket连接测试");
    println!("==========================");

    // 创建客户端
    let mut client = BinanceClientBuilder::new(
        "test_api_key".to_string(),
        "test_secret_key".to_string(),
        ProductType::Spot,
    )
    .environment(Environment::Demo)
    .timeout(30)
    .retry_count(3)
    .build()?;

    println!("✅ 客户端创建成功");
    println!("   REST URL: {}", client.config().rest_base_url());
    println!("   WebSocket URL: {}", client.config().ws_base_url());
    println!("   HTTP代理: {:?}", client.config().http_proxy);
    println!("   WebSocket代理: {:?}", client.config().ws_proxy);

    // 获取WebSocket客户端
    println!("🔌 获取WebSocket客户端...");
    let ws_client = client.websocket()?;
    println!("✅ WebSocket客户端获取成功");

    // 连接WebSocket
    println!("🔌 正在连接WebSocket...");
    match ws_client.connect().await {
        Ok(_) => {
            println!("✅ WebSocket连接成功！");
            
            // 等待一段时间让连接稳定
            println!("⏰ 等待连接稳定...");
            sleep(Duration::from_secs(2)).await;
            
            println!("🎉 WebSocket连接测试完成！");
        },
        Err(e) => {
            println!("❌ WebSocket连接失败: {}", e);
            return Err(e);
        }
    }

    // 关闭连接
    println!("🔌 关闭WebSocket连接...");
    ws_client.close().await?;
    println!("✅ WebSocket连接已关闭");

    Ok(())
}
